package com.tmc.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.baseinfo.Airport;
import com.tmc.service.baseinfo.AirportServiceImpl;
import com.tmc.web.BaseController;

@Controller
@RequestMapping("/airport")
public class AirportController extends BaseController {

	@Autowired
	private AirportServiceImpl airportService;
	
	/**
	 * 机场信息列表
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView findList(Airport cond, Integer pageIndex, Integer pageSize, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<Airport> page = airportService.findByCon(context, cond, pageIndex, pageSize);
		map.put("page", page);
		map.put("cond", cond);
		map.put("context", context);
		return new ModelAndView("views/baseInfo/airport-list", map);
	}
	
	@RequestMapping(value = "/search")
	@ResponseBody
	public String search(String query) {
		List<Airport> airports = airportService.search4Text(query);
		JSONArray array = new JSONArray();
		for (Airport airport : airports) {
			//"Key":"SHA","Name":"上海(虹桥)(SHA)"
			JSONObject obj = new JSONObject();
			obj.put("Key", airport.getAirportCode());
			obj.put("Name", airport.getCityName() + "(" + airport.getAirportCode() + ")");
			array.add(obj);
		}
		return array.toJSONString();
	}
	
	@RequestMapping(value = "/all", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String findAll() {
		List<Airport> all = airportService.findAllFromCache();
		return JSON.toJSONString(all);
	}
	
	@RequestMapping(value = "/editPage")
	public ModelAndView editPage(Long airportId, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		Airport airport = null;
		if (airportId != null)
			airport = airportService.getById(context, airportId);
		else
			airport = new Airport();
		map.put("airport", airport);
		map.put("context", context);
		return new ModelAndView("views/baseinfo/airport-edit", map);
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public String save(Airport airport) {
		CommonResult cr = airportService.save(new ContextInfo(), airport);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete(Long airportId) throws Exception {
		CommonResult cr = airportService.delete(new ContextInfo(), airportId);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/updateall", method = RequestMethod.POST)
	public ModelAndView updateall(HttpServletRequest request) throws Exception {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		airportService.updateAll(context);
		map.put("cond", new Airport());
		map.put("page", new Pagination<Airport>());
		map.put("context", context);
		return new ModelAndView("views/baseInfo/airport-list", map);
	}
	
	@RequestMapping(value = "/updateUnAirport", method = RequestMethod.POST)
	public ModelAndView updateUnAirport(HttpServletRequest request) throws Exception {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		airportService.updateUnAirport(context);
		map.put("cond", new Airport());
		map.put("page", new Pagination<Airport>());
		map.put("context", context);
		return new ModelAndView("views/baseInfo/airport-list", map);
	}
	
	@RequestMapping(value = "/updatefromcity", method = RequestMethod.POST)
	public ModelAndView updatefromcity(HttpServletRequest request) throws Exception {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		airportService.updateFromCity(context);
		map.put("cond", new Airport());
		map.put("page", new Pagination<Airport>());
		map.put("context", context);
		return new ModelAndView("views/baseInfo/airport-list", map);
	}
	
}
